<template>
  <div class="article-detail-page">
    <nav class="nav">
      <span @click="$router.back()" class="back">&lt;</span> 面经详情
    </nav>
    <header class="header">
      <h1>{{ article.stem }}</h1>
      <p>
        {{ article.createdAt }} | {{ article.views }} 浏览量 |
        {{ article.likeCount }} 点赞数
      </p>
      <p>
        <img :src="article.avatar" alt="" />
        <span>{{ article.creatorName }}</span>
      </p>
    </header>
    <main class="body" v-html='article.content'>
    </main>
    <div class="dingWei">
    <button class="yes"><van-icon size="30" name="like-o"  :class='{active:likeFlag}'  @click='postOptAdd(1)'/></button>
    <button> <van-icon size="30" name="star-o" :class='{active:collectFlag}'  @click='postOptAdd(2)'/></button>
    </div>
  </div>
</template>

<script>
import { getShow, postOpt } from '@/api/user'
export default {
  name: 'ArticleDetailPage',

  data () {
    return {
      article: [],
      collectFlag: false,
      likeFlag: false

    }
  },
  async created () {
    const res = await getShow(this.$route.params.id)
    this.article = res.data.data

    if (res.data.data.collectFlag === 1) {
      this.collectFlag = true
    } else {
      this.collectFlag = false
    }
    if (res.data.data.likeFlag === 1) {
      this.likeFlag = true
    } else {
      this.likeFlag = false
    }
  },
  methods: {
    async postOptAdd (optType) {
      const res = await postOpt(this.$route.params.id, optType)
      console.log(res, 1111111)
      if (optType === 1) {
        if (this.likeFlag === false) {
          this.likeFlag = true
          this.$toast('点赞成功')
        } else {
          this.likeFlag = false
          this.$toast('取消点赞')
        }
      } else {
        if (this.collectFlag === false) {
          this.collectFlag = true
          this.$toast('收藏成功')
        } else {
          this.collectFlag = false
          this.$toast('取消收藏')
        }
      }
    }
  }

}
</script>

<style lang="less" scoped>
.article-detail-page {
    position: absolute;
  .nav {
    height: 44px;
    border-bottom: 1px solid #e4e4e4;
    line-height: 44px;
    text-align: center;
    .back {
      font-size: 18px;
      color: #666;
      position: absolute;
      left: 10px;
      top: 0;
      transform: scale(1, 1.5);
    }
  }

  .header {
    padding: 0 15px;
    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
  .body {
    padding: 0 15px;
  }
  .dingWei{
    position: fixed;
    top: 74%;
    left: 57%;
    transform: translate(-20%, -50%);
    button{
    width: 50px;
    height: 50px;
    margin-left: 30px;
    background-color: #fff;
    border: 1px solid #e2e2e2;
    border-radius:50% ;
    }
    .active {
       width: #fff;
       background-color: orange;

  }
  }

}
</style>
